<?php
declare (strict_types = 1);

namespace app\command\logic;

use app\api\services\MemberService;
use app\api\services\SandService;
use app\command\logic\BaseLogic;
use app\common\models\Member\Member;
use app\common\models\Member\MemberAgency;
use app\common\models\Member\MemberRechargeOrder;
use app\common\models\Member\MemberWithdraw;
use DateInterval;
use DatePeriod;
use DateTime;
use think\console\Output;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Log;

/**
 * 统计-订单交易数据
 * php think cli stats_withdraw_data --msg='{"days":30}'
 */
class CliStatsWithdrawDataLogic extends BaseLogic implements CliInterface
{

    /**
     * 入口方法K
     * {@inheritdoc}
     */
    public function run(Output $output, array $msg): bool
    {
        if(isset($msg['date']) && $msg['date'] == 'today'){
            $this->dataForData(date('Y-m-d'));
        }elseif(isset($msg['date'])){
            $this->dataForData($msg['date']);
        }else{
            $this->dataForData(date('Y-m-d',strtotime('-1 day')));
        }

        return true;
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    private static function dataForData($date): void
    {
        $startTime = $date." 00:00:00";
        $endTime = $date." 23:59:59";

        $list = Member::getInstance()->where('deleted',0)->select();
        foreach($list as $v) {
            $m = MemberService::getInstance()->account($v['id']);
            if (empty($m) || $m['isActiveSd'] == 0) {
                continue;
            }
            $where = [
                ['user_id','=',$v['id']],
                ['trans_name','=','提现'],
                ['trans_time','between',[$startTime,$endTime]]
            ];
            $sand_to_card = \think\facade\Db::table('stats_sand_member_data')->where($where)->sum('act_amt') ?: "0.00";

            $where = [
                ['status','in',[0,1]],
                ['user_id','=',$v['id']],
                ['create_at','between',[$startTime,$endTime]]
            ];
            $amount = MemberWithdraw::getInstance()->where($where)->sum('amount') ?: '0.00';
            if($amount <= 0 && $sand_to_card <= 0){
                continue;
            }
            $data = [
                'real_name' => $m['realName'],
                'phone' => $v['phone'],
                'amount_tx' => $amount,
                'sand_to_card' => $sand_to_card,
                'user_id' => $v['id'],
                'date' => $date
            ];

            $where = [
                ['user_id','=',$v['id']],
                ['date','=',$date]
            ];
            $find = \think\facade\Db::table('stats_withdraw_data')->where($where)->find();
            if($find){
                $data['update_at'] = date('Y-m-d H:i:s');
                \think\facade\Db::table('stats_withdraw_data')->where('id',$find['id'])->update($data);
            }else{
                \think\facade\Db::table('stats_withdraw_data')->insert($data);
            }
        }



    }
}
